﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;

namespace TaskManagementSystem.Data
{
    public class DbContext : System.Data.Entity.DbContext
    {
        public DbContext() : base("DbConnectionString")
        {
            this.Configuration.AutoDetectChangesEnabled = false;
            this.Configuration.ValidateOnSaveEnabled = false;
            this.Configuration.LazyLoadingEnabled = false;
            this.Configuration.ProxyCreationEnabled = false;
        }

        public static DbContext CurrentContext
        {
            get
            {
                return new CurrentBase().DbContext;
            }
        }

        public class CurrentBase : IDisposable
        {
            [ThreadStatic]
            private DbContext db = null;

            public CurrentBase()
            {
                if (db == null)
                {
                    db = new DbContext();
                }
            }

            public DbContext DbContext
            {
                get
                {
                    return db;
                }
            }

            public void Dispose()
            {
                if (db != null)
                {
                    db.Dispose();
                    db = null;
                }
            }
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

        public DbSet<Entity.SystemCronConfigData> SystemCronConfigData { get; set; }

        public DbSet<Entity.SystemCronGroupData> SystemCronGroupData { get; set; }
    }
}